<?php

class Table {
  /** @var integer */
  public $id;
  /** @var integer */
  public $organizerId;
  /** @var sring */
  public $status;
  /** @var integer */
  public $maxPlayers;
  /** @var string */
  public $password;
  /** @var datetime */
  public $dateCreate;
  /** @var integer */
  public $gameId;
  
  const STATUS_NEW     = 'new';
  const STATUS_OPEN    = 'open';
  const STATUS_CLOSED  = 'closed';
  
  public function __construct($id = null, $name = null)
  {
    $this->Init();

    if (!$this->Load($id, $name))
      $this->Init();
  }

  private function Init()
  {
    $this->id = -1;
    $this->status = self::STATUS_NEW;
    $this->dateCreated = date('Y-m-d H:i:s');
  }
  
  private function Load($id = null)
  {
    if (!$this->LoadData($id)) return false;
    return true;
  }
  
  private function LoadData($id = null)
  {
    $where = 1;

    if (is_numeric($id))
      $where .= " AND id='" . $id . "'";
    else
      return false; 

    $query = "SELECT g.* "
            ." FROM games AS g"
            ." WHERE $where";

    $rows = DB::query($query);

    if (count($rows) < 1) return false;

    $data = $rows[0];
    $this->SetPropertyValues((array)$data);

    return true;
  }
  
  
  
}
?>
